#打印送货单
from msilib.schema import Font

import openpyxl
from openpyxl.styles import Font, Alignment, Border, Side, PatternFill

from entity.shipping_table_data_entity import ShippingTableDataEntity
from ut.set_save_path_config import get_sheet_address
from ut.set_shipment_config import read_shipment_config
from ut.windows_ui import input_file_name, show_warning


def print_shipment_table(shipping_list,date):
    print("打印送货单")
    str_NO=input_file_name("请输入单号(NO)：")

    #先计算数据
    shipment_ls=[]
    for ls in shipping_list:
        shipment_data=ShippingTableDataEntity(None,None,None,None,None)
        shipment_data.po=ls.po
        shipment_data.name=ls.name
        shipment_data.pcs_sum=ls.pcs_sum
        shipment_data.box_sum=ls.box_sum
        shipment_data.no="1/"+str(ls.box_sum)+"~"+str(ls.box_sum)+"/"+str(ls.box_sum)
        print(shipment_data.po,shipment_data.name,shipment_data.pcs_sum,shipment_data.box_sum,shipment_data.no)
        shipment_ls.append(shipment_data)

    #打印表格
    wb=openpyxl.Workbook()
    ws=wb.create_sheet("送货单")
    wb.remove(wb["Sheet"])
    #一，表格上段
    #列宽
    ws.column_dimensions['A'].width = 25
    ws.column_dimensions['B'].width = 21.56
    ws.column_dimensions['C'].width = 17.56
    ws.column_dimensions['D'].width = 11.44
    ws.column_dimensions['E'].width = 15.22
    #导入配置对象
    shipment_config=read_shipment_config()

    # 标题
    ws.merge_cells(start_row=1, start_column=1, end_row=1, end_column=5)
    #第一行高度
    ws.row_dimensions[1].height = 36.6
    #第一行文本
    ws.cell(row=1, column=1).value = shipment_config.customer_name
    #第一行样式
    ws.cell(row=1, column=1).font =Font(name='宋体', size=28, bold=True)
    ws.cell(row=1, column=1).alignment = Alignment(horizontal='center', vertical='center')
    #第二行高度
    ws.row_dimensions[2].height = 17.4
    #第二行文本
    ws.merge_cells(start_row=2, start_column=1, end_row=2, end_column=5)
    ws.cell(row=2, column=1).value = shipment_config.customer_addr
    #第二行样式
    ws.cell(row=2, column=1).font = Font(name='宋体', size=14, bold=True)
    ws.cell(row=2, column=1).alignment = Alignment(horizontal='center', vertical='center')
    #第三行高度
    ws.row_dimensions[3].height = 17.4
    #第三行文本
    ws.merge_cells(start_row=3, start_column=1, end_row=3, end_column=5)
    ws.cell(row=3, column=1).value = f"{shipment_config.TEL} {' '*6}{shipment_config.FAX}"
    #第三行样式
    ws.cell(row=3, column=1).font = Font(name='Times New Roman', size=14, bold=True)
    ws.cell(row=3, column=1).alignment = Alignment(horizontal='center', vertical='center')
    #第四行高度
    ws.row_dimensions[4].height = 36.6
    #第四行文本
    ws.merge_cells('A4:E4')
    ws.cell(row=4, column=1).value = f"送{' '*2}货{' '*2}单"
    #第四行样式
    ws.cell(row=4, column=1).font = Font(name='宋体', size=28, bold=True,underline='single')
    ws.cell(row=4, column=1).alignment = Alignment(horizontal='center', vertical='center')
    #第五行高度
    ws.row_dimensions[5].height = 31.5
    #第六行高度
    ws.row_dimensions[6].height = 20.5
    #第六行文本
    ws.merge_cells('A6:B6')
    ws.cell(row=6, column=1).value = '客户名称：' + shipment_config.customer_name
    # 第六行样式
    ws.cell(row=6, column=1).font = Font(name='宋体', size=14, bold=True)
    ws.cell(row=6, column=1).alignment = Alignment(horizontal='left', vertical='center')
    #第七行高度
    ws.row_dimensions[7].height = 20.5
    #第七行文本
    ws.merge_cells('A7:C7')
    ws.cell(row=7, column=1).value = '地址：' + shipment_config.customer_addr
    #第七行样式
    ws.cell(row=7, column=1).font = Font(name='宋体', size=12, bold=True)
    ws.cell(row=7, column=1).alignment = Alignment(horizontal='left', vertical='center')
    #第八行高度
    ws.row_dimensions[8].height = 20.5
    #第九行高度
    ws.row_dimensions[9].height = 20.5
    #第九行第一格文本
    ws.cell(row=9, column=1).value = '联系人：' + shipment_config.linkman
    #第九行第一格样式
    ws.cell(row=9, column=1).font = Font(name='宋体', size=12, bold=True)
    ws.cell(row=9, column=1).alignment = Alignment(horizontal='left', vertical='center')
    #第九行第四格文本
    ws.cell(row=9, column=4).value = "NO:"
    #第九行第四格样式
    ws.cell(row=9, column=4).font = Font(name='宋体', size=14, bold=True)
    ws.cell(row=9, column=4).alignment = Alignment(horizontal='center', vertical='center')
    #第九行第五格文本
    ws.cell(row=9, column=5).value = str_NO
    ws.cell(row=9, column=5).font = Font(name='宋体', size=14, bold=True)
    ws.cell(row=9, column=5).alignment = Alignment(horizontal='center', vertical='center')
    #第十行第一格文本
    ws.cell(row=10, column=1).value = "联系电话："+str(shipment_config.contact_number)
    ws.cell(row=10, column=1).font = Font(name='宋体', size=12, bold=True)
    ws.cell(row=10, column=1).alignment = Alignment(horizontal='left', vertical='center')
    #第十行第四格文本
    ws.cell(row=10, column=4).value = "DATE："
    ws.cell(row=10, column=4).font = Font(name='宋体', size=14, bold=True)
    ws.cell(row=10, column=4).alignment = Alignment(horizontal='center', vertical='center')
    #第十行第五格文本
    ws.cell(row=10, column=5).value = date
    ws.cell(row=10, column=5).font = Font(name='宋体', size=14, bold=True)
    ws.cell(row=10, column=5).alignment = Alignment(horizontal='center', vertical='center')

    #二，表格中段
    #记录开始行和结束行
    start_row = 11
    end_row = len(shipment_ls)+start_row+1
    #指针行
    p_row = start_row+1
    #表头
    ws.cell(row=start_row, column=1).value = 'ITEM'
    ws.cell(row=start_row, column=2).value = 'DESCRIPTION'
    ws.cell(row=start_row, column=3).value = 'QTY(PCS)'
    ws.cell(row=start_row, column=4).value = 'C/T'
    ws.cell(row=start_row, column=5).value = 'NO'
    #表格内容
    for ls in shipment_ls:
        ws.cell(row=p_row, column=1).value = ls.po
        ws.cell(row=p_row, column=2).value = ls.name
        ws.cell(row=p_row, column=3).value = ls.pcs_sum
        ws.cell(row=p_row, column=4).value = ls.box_sum
        ws.cell(row=p_row, column=5).value = ls.no
        p_row += 1
    #合计
    ws.cell(row=p_row, column=2).value = '合计'
    ws.cell(row=p_row, column=3).value = sum([shipment_ls[i].pcs_sum for i in range(len(shipment_ls))])
    ws.cell(row=p_row, column=4).value = sum([shipment_ls[i].box_sum for i in range(len(shipment_ls))])

    #表格样式
    for i in range(start_row,p_row+1):
        #行高
        ws.row_dimensions[i].height = 30
        for j in range(1,6):
            ws.cell(row=i, column=j).font = Font(name='宋体', size=14, bold=True)
            ws.cell(row=i, column=j).alignment = Alignment(horizontal='center', vertical='center')
            ws.cell(row=i, column=j).border =  Border(left=Side(border_style='thin', color='000000'),
                                 right=Side(border_style='thin', color='000000'),
                                 top=Side(border_style='thin', color='000000'),
                                 bottom=Side(border_style='thin', color='000000'))
    #三，表格下段
    p_row=p_row+1
    ws.merge_cells(start_row=p_row, start_column=1, end_row=p_row, end_column=2)
    ws.cell(row=p_row, column=1).value = '          Received  by'
    ws.cell(row=p_row, column=1).font = Font(name='Times New Roman', size=18, bold=True)
    ws.cell(row=p_row, column=1).alignment = Alignment(horizontal='left', vertical='center')
    ws.merge_cells(start_row=p_row, start_column=3, end_row=p_row, end_column=4)
    ws.cell(row=p_row, column=3).value = 'Issued  by'
    ws.cell(row=p_row, column=3).font = Font(name='Times New Roman', size=18, bold=True)
    ws.cell(row=p_row, column=3).alignment = Alignment(horizontal='center', vertical='center')
    #行高
    for i in range(p_row, p_row+4):
        ws.row_dimensions[i].height = 22.8
    p_row = p_row+2
    ws.merge_cells(start_row=p_row, start_column=3, end_row=p_row, end_column=4)
    ws.cell(row=p_row, column=3).value = shipment_config.person_in_charge
    ws.cell(row=p_row, column=3).font = Font(name='华文新魏', size=18, bold=True)
    ws.cell(row=p_row, column=3).alignment = Alignment(horizontal='center', vertical='center')

    p_row = p_row+1
    ws.merge_cells(start_row=p_row, start_column=3, end_row=p_row, end_column=4)
    ws.cell(row=p_row, column=3).value ="____________________________"
    ws.cell(row=p_row, column=3).font = Font(name='宋体', size=11)

    #保存
    file_path=get_sheet_address()
    file_path=file_path+"\\"+date+"送货单.xlsx"
    try:
        wb.save(file_path)
        show_warning("送货单已保存至："+ file_path)
    except Exception as e:
        show_warning("送货单保存失败，请检查相关文件是否被占用。")
        exit(e)








